﻿using Mybook.Models;
using MyStory.Service;
using MyStory.util;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Mybook.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class SearchDetailPageDetail : ContentPage
    {
        private FictionService _fictionService;
        ObservableCollection<string> titles;
        private List<FictionModel> repositoriesinit;

        public SearchDetailPageDetail()
        {
            InitializeComponent();
            _fictionService = new FictionService();
            init();
            //searchResults.ItemsSource = new object[1, 2];

        }
        private async void init()
        {
            repositoriesinit = await _fictionService.GetRepositoriesAsync(ConnectUrl.GetSearchUrl("title", "方"));
            titles = new ObservableCollection<string>();
            foreach (var item in repositoriesinit)
            {
                titles.Add(item.Title);

            }
            searchResults.ItemsSource = titles;
           
        }

        private void searchResults_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            searchBar.Text=e.SelectedItem.ToString();
            searchResults.IsVisible = false;
           // searchResults.IsEnabled = false;
            collectionView.ItemsSource = repositoriesinit;
        }
        private async void OnButtonClicked(object sender, EventArgs e)
        {
            searchResults.IsVisible = false;

            List<FictionModel> repositories0 = new List<FictionModel>();
            List<FictionModel> repositories = await _fictionService.GetRepositoriesAsync(ConnectUrl.GetSearchUrl("title", searchBar.Text));
            List<FictionModel> repositories1 = await _fictionService.GetRepositoriesAsync(ConnectUrl.GetSearchUrl("author", searchBar.Text));
            List<FictionModel> repositories2 = await _fictionService.GetRepositoriesAsync(ConnectUrl.GetSearchUrl("fictionType", searchBar.Text));
            if (repositories != null)
            {
                repositories0.AddRange(repositories);
            }
            if (repositories1 != null)
            {
                repositories0.AddRange(repositories1);
            }
            if (repositories2 != null)
            {
                repositories0.AddRange(repositories2);
            }

            collectionView.ItemsSource = repositories0;
        }
        private async void OnButtonClicked1(object sender, EventArgs e)
        {
            List<FictionModel> repositories0 = new List<FictionModel>();
            List<FictionModel> repositories = await _fictionService.GetRepositoriesAsync(ConnectUrl.GetSearchUrl("title", searchBar.Text, size: 1000));
            List<FictionModel> repositories1 = await _fictionService.GetRepositoriesAsync(ConnectUrl.GetSearchUrl("author", searchBar.Text, size: 1000));
            List<FictionModel> repositories2 = await _fictionService.GetRepositoriesAsync(ConnectUrl.GetSearchUrl("fictionType", searchBar.Text, size: 1000));
            if (repositories != null)
            {
                repositories0.AddRange(repositories1);
            }
            if (repositories1 != null)
            {
                repositories.AddRange(repositories1);
            }
            if (repositories2 != null)
            {
                repositories.AddRange(repositories2);
            }
            collectionView.ItemsSource = repositories;



        }
        public static string fictionid;

        private async void collectionView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            FictionModel model = (FictionModel)e.SelectedItem;
            await Navigation.PushAsync(page: new BookDetail(model));
        }
        ViewCell lastCell;
        private void ViewCell_Tapped(object sender, EventArgs e)
        {
            if (lastCell != null)
                lastCell.View.BackgroundColor = Color.AntiqueWhite;
            var viewCell = (ViewCell)sender;
            if (viewCell.View != null)
            {
                viewCell.View.BackgroundColor = Color.AliceBlue;
                lastCell = viewCell;
            }
        }
        public void OnMore(object sender, EventArgs e)
        {
            var mi = ((MenuItem)sender);
            DisplayAlert("More Context Action", mi.CommandParameter + " more context action", "OK");
        }

        public void OnDelete(object sender, EventArgs e)
        {
            var mi = ((MenuItem)sender);
            DisplayAlert("Delete Context Action", mi.CommandParameter + " delete context action", "OK");
        }
    }
}